package com.example.androidthings.myproject.applist;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.app.Activity;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;

import com.example.androidthings.myproject.R;

public class AppListActivity extends Activity {

	ListView appListView = null;

	List<AppData> appDataList = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_app_list);

		// init data
		appDataList = initAPPDataList();
		// init view
		appListView = (ListView) this.findViewById(R.id.app_ListView);
		appListView.setAdapter(new AppListAdapter(this, appDataList));
		appListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				
				
				AppListActivity.this.startActivity(getPackageManager()
						.getLaunchIntentForPackage(
								appDataList.get(arg2).appPackageName));

			}
		});

	}


	public List<AppData> initAPPDataList() {

		List<AppData> appDataList = new ArrayList<AppData>();

		PackageManager pm = getPackageManager();

		List<PackageInfo> packs = pm.getInstalledPackages(0);
		for (PackageInfo pi : packs) {
			AppData appData = new AppData(pi.applicationInfo.loadLabel(pm),
					pi.applicationInfo.packageName,
					pi.applicationInfo.loadIcon(pm));
			appDataList.add(appData);

		}
		return appDataList;
	}

}
